import 'dart:async';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/animation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DrawBeiSaierWidget extends StatefulWidget {
  DrawBeiSaierWidget();

  @override
  _DrawBeiSaierWidgetState createState() => _DrawBeiSaierWidgetState();
}

class _DrawBeiSaierWidgetState extends State<DrawBeiSaierWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
          child: RepaintBoundary(
        //隔离重绘控件，singlescrollview 滑动发生重绘，导致画笔paint响应
        child: Container(
            width: 200,
            height: 200,
            color: Color(0xFFB19F84),
            child: CustomPaint(painter: _BeiSaierPainter())),
      )),
    );
  }

  ///建立比例颜色

  @override
  void dispose() {
    super.dispose();
  }
}

class _BeiSaierPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final side = min(size.width, size.height);
    Paint _painter = Paint()
      ..color = Color(0xFFD74646)
      ..style = PaintingStyle.stroke
      ..isAntiAlias = true;

    var path = Path();
    path.moveTo(0, 50);
    // path.quadraticBezierTo(50,100,100,50);
    // path.quadraticBezierTo(170,20,200,50);
    // path.cubicTo(50, 100, 150, 50, 200, 100);
    // canvas.drawPath(path, _painter);
    paintLove(canvas,_painter,size);
  }

  Offset get3OrderBezierPoint(
      Offset p1, Offset p2, Offset p3, Offset p4, double t) {
    var x = (1 - t) * (1 - t) * (1 - t) * p1.dx +
        3 * t * (1 - t) * (1 - t) * p2.dx +
        3 * t * t * (1 - t) * p3.dx +
        t * t * t * p4.dx;
    var y = (1 - t) * (1 - t) * (1 - t) * p1.dy +
        3 * t * (1 - t) * (1 - t) * p2.dy +
        3 * t * t * (1 - t) * p3.dy +
        t * t * t * p4.dy;

    return Offset(x, y);
  }

  void paintLove(Canvas canvas, Paint paint, Size size) {

    var path = Path();
    path.moveTo(0,50);
    for (var t = 1; t <= 100; t += 1) {
      var curvePoint = get3OrderBezierPoint(Offset(0,50), Offset(50,100), Offset(150,50), Offset(200,100), t / 100.0);

    path.lineTo(curvePoint.dx, curvePoint.dy);
    }

    // paint.color = Colors.red[400]!;
    // canvas.drawPath(path5, paint);
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant _BeiSaierPainter oldDelegate) {
    return true;
  }
}
